from flask import Blueprint, make_response
from flask_restful import Api, Resource
from app.route import routes

app_api = Blueprint('app_api', __name__)

restful_api = Api()
restful_api.init_app(app_api)


for item in routes:
	restful_api.add_resource(item["resource"], item["url"], endpoint=item["endpoint"])


@restful_api.representation('application/json')
def output_json(data, code, headers=None):
	if code not in [0, 200]:
		msg = data["message"]
		if "errcode"in msg.keys():
			res = {"code": msg["errcode"], "message": msg["errmsg"]}
		else:
			res = {"code": code, "message": data["message"]}
	else:
		if "data" in data:
			res = {"code": 200, "message": "ok", "data": data["data"]}
		res = {"code": 200, "message": "ok", "data": data}

	resp = make_response(res, code)
	return resp
